/**
 * Copyright &copy; 2012-2016 <a href="https://shop450532966.taobao.com/">NumberOne</a> All rights reserved.
 */
package com.numberone.master.modules.people.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.numberone.master.modules.people.entity.ReservePerson;
import com.numberone.master.modules.people.service.ReservePersonService;
import com.numberone.master.modules.sys.entity.Office;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.numberone.master.common.config.Global;
import com.numberone.master.common.persistence.Page;
import com.numberone.master.common.web.BaseController;
import com.numberone.master.common.utils.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * 单表生成Controller
 *
 * @author DengWenJie
 * @version 2017-11-21
 */
@Controller
@RequestMapping(value = "${adminPath}/people/reservePerson")
public class ReservePersonController extends BaseController {

  @Autowired
  private ReservePersonService reservePersonService;

  @ModelAttribute
  public ReservePerson get(@RequestParam(required = false) String id) {
    ReservePerson entity = null;
    if (StringUtils.isNotBlank(id)) {
      entity = reservePersonService.get(id);
    }
    if (entity == null) {
      entity = new ReservePerson();
    }
    return entity;
  }

  @RequiresPermissions("people:reservePerson:view")
//	@RequestMapping(value = {"list", ""})
  @RequestMapping(value = {"list", ""}, method = {RequestMethod.GET, RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_VALUE)
  public String list(ReservePerson ReservePerson, HttpServletRequest request, HttpServletResponse response, Model model) {
    Page<ReservePerson> page = reservePersonService.findPage(new Page<ReservePerson>(request, response), ReservePerson);
    model.addAttribute("page", page);
    return "modules/people/reservePersonList";
  }

  @RequiresPermissions("people:reservePerson:view")
//	@RequestMapping(value = {"list", ""})
  @RequestMapping(value = {"listData"})
  @ResponseBody
  public Page<ReservePerson> listData(ReservePerson ReservePerson, HttpServletRequest request, HttpServletResponse response, Model model) {
    Page<ReservePerson> page = reservePersonService.findPage(new Page<ReservePerson>(request, response), ReservePerson);
    return page;
  }

  @RequiresPermissions("people:reservePerson:view")
  @RequestMapping(value = "form")
  public String form(ReservePerson ReservePerson, Model model) {
    model.addAttribute("ReservePerson", ReservePerson);
    return "modules/people/reservePersonForm";
  }

  @RequiresPermissions("people:reservePerson:edit")
  @RequestMapping(value = "save")
  public String save(ReservePerson ReservePerson, Model model, RedirectAttributes redirectAttributes) {
    if (!beanValidator(model, ReservePerson)) {
      return form(ReservePerson, model);
    }
    reservePersonService.save(ReservePerson);
    addMessage(redirectAttributes, "保存单表成功");
    return "redirect:" + Global.getAdminPath() + "/people/ReservePerson/?repage";
  }

  @RequiresPermissions("people:reservePerson:edit")
  @RequestMapping(value = "delete")
  public String delete(ReservePerson ReservePerson, RedirectAttributes redirectAttributes) {
    reservePersonService.delete(ReservePerson);
    addMessage(redirectAttributes, "删除单表成功");
    return "redirect:" + Global.getAdminPath() + "/people/ReservePerson/?repage";
  }

  @ResponseBody
  @RequestMapping(value = "allList")
  public List allList(@RequestParam(required=false) String extId, @RequestParam(required=false) String type,
                      @RequestParam(required=false) Long grade, @RequestParam(required=false) Boolean isAll, HttpServletResponse response) {
    List<Map<String, Object>> mapList = Lists.newArrayList();
    List<ReservePerson> list = reservePersonService.findList(new ReservePerson());
    for (int i = 0; i < list.size(); i++) {
      Map<String, Object> map = Maps.newHashMap();
      map.put("id", list.get(i).getUserNo());
      map.put("pId", 0);
      map.put("pIds", "0,");
      map.put("name", list.get(i).getRemarks());
      mapList.add(map);
    }
    return mapList;
  }

}